package com.thoughtworks.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.model.Constants;
import com.thoughtworks.model.Edge;

public class ParseInputUtil {
	
	/**
	 * @param input_file  : input file that describe the graph
	 * @return            : all edges of the graph
	 * @throws IOException
	 */
	public static List<Edge> getEdgeList(String input_file) throws IOException {
		String content = ReadFileUtil.getFileContent(input_file);
		List<String> nodeList = parseInputContent(content);
		List<Edge> edgeList = new ArrayList<Edge>();
		for(String node : nodeList) {
			if("".equals(node.trim()) || node.trim().startsWith(Constants.COMMENT_SYMBOL)){
				continue;
			}
			edgeList.add(new Edge(node.substring(0, 1), node.substring(1, 2), Integer.parseInt(node.substring(2, 3))));
		}
		return edgeList;
	}
	
	private static List<String> parseInputContent(String content) {
		if(content.contains(":")) {
			int index = content.indexOf(":") + 1;
			content = content.substring(index);
		}
		String[] nodes = content.split(",");
		List<String> nodeList = new ArrayList<String>();
		for(String str : nodes) {
			nodeList.add(str.trim());
		}
		return nodeList;
	}
}
